package com.lagou.edu.course.controller;

import com.lagou.edu.course.api.dto.CourseDTO;
import com.lagou.edu.course.api.dto.PageResultDTO;
import com.lagou.edu.course.api.param.CourseQueryParam;
import com.lagou.edu.course.remote.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/course")
public class CourseController {
    @Autowired
    private CourseService courseService;

    /**
     * 获取选课列表
     *
     * @param userId
     * @return
     */
    @GetMapping("/getAllCourses")
    public List<CourseDTO> getAllCourses(@RequestParam(required = false, name = "userId") Integer userId) {
        return courseService.getAllCourses(userId);
    }

    @GetMapping("/getCourseById")
    @ResponseBody
    public CourseDTO getCourseById(@RequestParam("courseId") Integer courseId,
                                   @RequestParam("userId") Integer userId) {
        log.info("getCourseById    courseId-{},userId-{}", courseId, userId);
        return courseService.getCourseById(courseId);
    }

    /**
     * 新增或则修改课程信息
     *
     * @param courseDTO
     * @return 成功或则失败  true成功 false失败
     */
    @PostMapping(value = "/saveOrUpdateCourse")
    @ResponseBody
    public boolean saveOrUpdateCourse(@RequestBody CourseDTO courseDTO, @RequestParam("userId") Integer userId) {
        log.info("saveOrUpdateCourse    courseId-{},userId-{}", courseDTO.getId(), userId);
        return courseService.saveCourse(courseDTO, userId);
    }

    /**
     * 获取已购课程信息
     *
     * @param userId
     * @return
     */
    @GetMapping("/getPurchasedCourse")
    List<CourseDTO> getPurchasedCourse(@RequestParam("userId") Integer userId) {
        return courseService.getPurchasedCourse(userId);
    }

    @PostMapping(value = "/getQueryCourses")
    PageResultDTO<CourseDTO> getQueryCourses(@RequestBody CourseQueryParam courseQueryParam) {
        return courseService.getQueryCourses(courseQueryParam);
    }

}
